<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Listener;

use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Consul\Agent;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Http\Server\HttpServer;
use Swoft\Log\Helper\CLog;
use Swoft\Server\SwooleEvent;

/**
 * Class RegisterServiceListener
 *
 * @since 2.0
 *
 * @Listener(event=SwooleEvent::START)
 */
class RegisterServiceListener implements EventHandlerInterface
{
    /**
     * @Inject()
     *
     * @var Agent
     */
    private $agent;

    /**
     * @param EventInterface $event
     */
    public function handle(EventInterface $event): void
    {
        /** @var HttpServer $httpServer */
        $httpServer = $event->getTarget();

        $host       = env('IP_TO_REGISTRY', '127.0.0.1');
        $port       = $httpServer->getPort();


        $appName        = env('APP_NAMRE', 'swoft-app');
        $serviceId      = "{$appName}-{$host}:{$port}";
        $serviceName    = $appName;

        $service = [
            'ID'                => $serviceId,
            'Name'              => $serviceName,
            'Tags'              => [
                'http'
            ],
            'Address'           => $host,
            'Port'              => $port,
            'check'             => [ // 心跳检查
                'id'            => 'api',
                'name'          => 'HTTP API on port '. $port,
                "http"          => "http://{$host}:{$port}/health", //
                "deregisterCriticalServiceAfter" => "30s",// check失败后10秒删除本服务
                "interval"      => "10s",
                "timeout"       => "1s"
            ],
            'Meta'              => [
                'version'       => '1.0',
                'RpcPort'       => env("RPC_PORT", 18307),
            ],
            'EnableTagOverride' => false,
            'Weights'           => [
                'Passing'       => 10,
                'Warning'       => 1
            ],
        ];
        // Register
        $this->agent->registerService($service);
        CLog::info('Swoft http register service success by consul!');
    }
}
